<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="">
        <input type="text" name="oldpassword" />
        <input type="text" name="newpassword" />
        <button>确认修改</button>
    </form>
    <script>
        //获取元素 
        const form = document.querySelector('form');
        const oldpassword = document.querySelector('input[name=oldpassword]');
        const newpassword = document.querySelector('input[name=newpassword]');


        //监听表单提交事件
        form.onsubmit = function(e){
            e.preventDefault();
            //获取表单数据
            const data = {
                oldpassword: oldpassword.value,
                newpassword: newpassword.value,
                // 获取本地存储的id 我好知道改谁的密码
                id: window.localStorage.getItem('id')
            }

            //发送请求
            const xhr = new XMLHttpRequest();
            xhr.open('POST','/api/users/rpwd');
            xhr.onload = function(){
                const res = JSON.parse(xhr.responseText);
                console.log(res);
                // 获取 id和 token  

                // 保存到本地存储中 

            }
            xhr.setRequestHeader('Content-Type','application/json');
            xhr.setRequestHeader('authorization',window.localStorage.getItem('token'));
            xhr.send(JSON.stringify(data));
        }
    </script>
</body>
</html>